package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.SundayActivityRelated;
import org.itboys.mobile.service.mongo.web.SundayActivityRelationService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author：贾琦磊
 * @Date：2018/7/18
 * @Description：活动相关
 */
@Controller
@RequestMapping("/sunday/web/activity/related")
public class SundayActivityRelatedController extends BaseController{
    @Autowired
    private SundayActivityRelationService relationService;
    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response
                        ,@RequestParam("activityId")Long activityId
                         , Model model){
        model.addAttribute("activityId",activityId);
        return "sunday/activity/related/index";
    }
    @RequestMapping("/select")
    public void select(HttpServletRequest request, HttpServletResponse response
    , @RequestParam("activityId") Long activityId){
        try {
            PageResult<SundayActivityRelated> result = relationService.select(request,activityId);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    @RequestMapping("/input")
    public String input(HttpServletRequest request, HttpServletResponse response
    ,@RequestParam("activityId")Long activityId
    ,Model model){
        model.addAttribute("activityId",activityId);
            return "sunday/activity/related/input";
    }
    @RequestMapping("/save")
    public void save(HttpServletRequest request, HttpServletResponse response
            , @RequestParam("activityId")Long activityId
            , @RequestParam("otherId")Long otherId
            , @RequestParam("type")Integer type
           ){
        try {
            relationService.save(activityId,otherId,type);
            ResponseMessage.success();
        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/activity/related/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }
    }

    /**
     * 删除相关活动
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,HttpServletResponse response
            ,@RequestParam("id") Long id){
        try {
            relationService.delete(id);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,"error");
        }
    }
}
